package com.shop2cn.iapi.prodstock.testcase;

import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.shop2cn.iapi.prodstock.parameter.OpenAdjustStockBean;
import com.shop2cn.iapi.prodstock.service.OpenAdjustStockCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.DetailsCatalogDto;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.shop2cn.iapi.agentproducts.parameter.UpdateStockDto;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.Shop2cnStockCallService;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;
/**
*OpenAPI调整商品库存Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_OpenAdjustStock{
	private static OpenAdjustStockBean openadjuststockBean;
	private static OpenAdjustStockCall openadjuststockCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("OpenAPI调整商品库存Test");
	}

	@Before
	public void caseUp() {
		openadjuststockBean=new OpenAdjustStockBean();
		openadjuststockCall=new OpenAdjustStockCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(openadjuststockCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	// 渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenAdjustStock_001() {
		Logger.start(true,"正确数据，成功");
		try {
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			
			openadjuststockBean.setUserId(rootUserId);
			openadjuststockBean.setBizNo(TestDataManager.getRandomNum(36));
			openadjuststockBean.setUpdateStockList(Arrays.asList(new UpdateStockDto(productId,null, catalogId, 10, -10)));
			openadjuststockCall.setData(openadjuststockBean);
			openadjuststockCall.callService();			
			Logger.verifyEquals("200", openadjuststockCall.getString("Code"), "验证code");
			
			ProductDetailsDto productDetailsDto1 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			DetailsCatalogDto catalog = productDetailsDto1.getCatalogList().get(0);
			Logger.verifyEquals(70,catalog.getStock(), "验证总库存");
			Logger.verifyEquals(50,catalog.getRealStock(),"验证真实库存");
			Logger.verifyEquals(20,catalog.getVirtualStock(), "验证虚拟库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenAdjustStock_002() {
		Logger.start(true,"库存不够，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			
			openadjuststockBean.setUserId(rootUserId);
			openadjuststockBean.setBizNo(TestDataManager.getRandomNum(36));
			openadjuststockBean.setUpdateStockList(Arrays.asList(new UpdateStockDto(productId,null, catalogId, 10, -60)));
			openadjuststockCall.setData(openadjuststockBean);
			openadjuststockCall.callService();			
			Logger.verifyEquals("201", openadjuststockCall.getString("Code"), "验证code");
			Logger.verifyIsContains("商品库存不足", openadjuststockCall.getString("Msg"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenAdjustStock_003() {
		Logger.start(true,"catalog不存在，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			
			openadjuststockBean.setUserId(rootUserId);
			openadjuststockBean.setBizNo(TestDataManager.getRandomNum(36));
			openadjuststockBean.setUpdateStockList(Arrays.asList(new UpdateStockDto(productId,null, catalogId+"c", 10, -60)));
			openadjuststockCall.setData(openadjuststockBean);
			openadjuststockCall.callService();			
			Logger.verifyEquals("201", openadjuststockCall.getString("Code"), "验证code");
			Logger.verifyIsContains("规格编号不存在", openadjuststockCall.getString("Msg"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenAdjustStock_004() {
		Logger.start(true,"多个规格，其中一个更新失败，回滚操作");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(1).setStock(20);
			addAgentProductBean.getCatalogList().get(1).setVirtualStock(10);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			String catalogId1 = productDetailsDto.getCatalogList().get(1).getCatalogId();
			
			openadjuststockBean.setUserId(rootUserId);
			openadjuststockBean.setBizNo(TestDataManager.getRandomNum(36));
			openadjuststockBean.setUpdateStockList(Arrays.asList(new UpdateStockDto(null, catalogId, 10, -10),new UpdateStockDto(null, catalogId1, 10, -10)));
			openadjuststockCall.setData(openadjuststockBean);
			openadjuststockCall.callService();			
			Logger.verifyEquals("200", openadjuststockCall.getString("Code"), "验证code");
			ProductDetailsDto productDetailsDto1 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			DetailsCatalogDto catalog = productDetailsDto1.getCatalogList().get(0);
			Logger.verifyEquals(70,catalog.getStock(), "验证总库存");
			Logger.verifyEquals(50,catalog.getRealStock(),"验证真实库存");
			Logger.verifyEquals(20,catalog.getVirtualStock(), "验证虚拟库存");
			DetailsCatalogDto catalog1 = productDetailsDto1.getCatalogList().get(1);
			Logger.verifyEquals(30,catalog1.getStock(), "验证总库存");
			Logger.verifyEquals(30,catalog1.getRealStock(),"验证真实库存");
			Logger.verifyEquals(0,catalog1.getVirtualStock(), "验证虚拟库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenAdjustStock_005() {
		Logger.start(true,"多个规格，其中一个更新失败，回滚操作");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(1).setStock(20);
			addAgentProductBean.getCatalogList().get(1).setVirtualStock(10);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			String catalogId1 = productDetailsDto.getCatalogList().get(1).getCatalogId();
			
			openadjuststockBean.setUserId(rootUserId);
			openadjuststockBean.setBizNo(TestDataManager.getRandomNum(36));
			openadjuststockBean.setUpdateStockList(Arrays.asList(new UpdateStockDto(null, catalogId, 10, -10),new UpdateStockDto(null, catalogId1, 10, -20)));
			openadjuststockCall.setData(openadjuststockBean);
			openadjuststockCall.callService();			
			Logger.verifyEquals("201", openadjuststockCall.getString("Code"), "验证code");
			Logger.verifyIsContains("商品库存不足", openadjuststockCall.getString("Msg"), "验证code");
			ProductDetailsDto productDetailsDto1 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			DetailsCatalogDto catalog = productDetailsDto1.getCatalogList().get(0);
			Logger.verifyEquals(70,catalog.getStock(), "验证总库存");
			Logger.verifyEquals(40,catalog.getRealStock(),"验证真实库存");
			Logger.verifyEquals(30,catalog.getVirtualStock(), "验证虚拟库存");
			DetailsCatalogDto catalog1 = productDetailsDto1.getCatalogList().get(1);
			Logger.verifyEquals(30,catalog1.getStock(), "验证总库存");
			Logger.verifyEquals(20,catalog1.getRealStock(),"验证真实库存");
			Logger.verifyEquals(10,catalog1.getVirtualStock(), "验证虚拟库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenAdjustStock_006() {
		Logger.start(true,"使用out_skuid更新，成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			String sku = productDetailsDto.getCatalogList().get(0).getSku();
			
			openadjuststockBean.setUserId(rootUserId);
			openadjuststockBean.setBizNo(TestDataManager.getRandomNum(36));
			openadjuststockBean.setUpdateStockList(Arrays.asList(new UpdateStockDto(productId,sku, null, 10, -10)));
			openadjuststockCall.setData(openadjuststockBean);
			openadjuststockCall.callService();			
			Logger.verifyEquals("200", openadjuststockCall.getString("Code"), "验证code");
			ProductDetailsDto productDetailsDto1 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			DetailsCatalogDto catalog = productDetailsDto1.getCatalogList().get(0);
			Logger.verifyEquals(70,catalog.getStock(), "验证总库存");
			Logger.verifyEquals(50,catalog.getRealStock(),"验证真实库存");
			Logger.verifyEquals(20,catalog.getVirtualStock(), "验证虚拟库存");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenAdjustStock_007() {
		Logger.start(true,"当skuid与商户不匹配，修改失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			String sku = productDetailsDto.getCatalogList().get(0).getSku();
			
			openadjuststockBean.setUserId(rootUserId+1);
			openadjuststockBean.setBizNo(TestDataManager.getRandomNum(36));
			openadjuststockBean.setUpdateStockList(Arrays.asList(new UpdateStockDto(productId,sku, null, 10, -10)));
			openadjuststockCall.setData(openadjuststockBean);
			openadjuststockCall.callService();			
			Logger.verifyEquals("201", openadjuststockCall.getString("Code"), "验证code");
			Logger.verifyIsContains("sku_id无效", openadjuststockCall.getString("Msg"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenAdjustStock_008() {
		Logger.start(true,"当catalogId与商户不匹配，修改失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			
			openadjuststockBean.setUserId(rootUserId+1);
			openadjuststockBean.setBizNo(TestDataManager.getRandomNum(36));
			openadjuststockBean.setUpdateStockList(Arrays.asList(new UpdateStockDto(productId,null, catalogId, 10, -10)));
			openadjuststockCall.setData(openadjuststockBean);
			openadjuststockCall.callService();			
			Logger.verifyEquals("201", openadjuststockCall.getString("Code"), "验证code");
			Logger.verifyIsContains("规格Id和此商户不匹配", openadjuststockCall.getString("Msg"), "验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenAdjustStock_009() {
		Logger.start(true,"修改后验证入库存盘点");
		try {
			
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			openadjuststockBean.setUserId(rootUserId);
			openadjuststockBean.setBizNo(TestDataManager.getRandomNum(36));
			openadjuststockBean.setUpdateStockList(Arrays.asList(new UpdateStockDto(productId,null, catalogId, 30, -20)));
			openadjuststockCall.setData(openadjuststockBean);
			openadjuststockCall.callService();			
			Logger.verifyEquals("200", openadjuststockCall.getString("Code"), "验证code");
			ProductDetailsDto productDetailsDto1 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			DetailsCatalogDto catalog = productDetailsDto1.getCatalogList().get(0);
			Logger.verifyEquals(80,catalog.getStock(), "验证总库存");
			Logger.verifyEquals(70,catalog.getRealStock(),"验证真实库存");
			Logger.verifyEquals(10,catalog.getVirtualStock(), "验证虚拟库存");
			JsonObject in = Shop2cnStockCallService.GetStockInInventoryList(rootUserId).get(0).getAsJsonObject();
			Logger.verifyEquals(30, in.get("totalNum").getAsInt(),"验证入库");
			
		} catch (Exception e) {
			Logger.fail(e);
	}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_OpenAdjustStock_010() {
		Logger.start(true,"修改后验证出库存盘点");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			
			
			openadjuststockBean.setUserId(rootUserId);
			openadjuststockBean.setBizNo(TestDataManager.getRandomNum(36));
			openadjuststockBean.setUpdateStockList(Arrays.asList(new UpdateStockDto(productId,null, catalogId, -30, 20)));
			openadjuststockCall.setData(openadjuststockBean);
			openadjuststockCall.callService();			
			Logger.verifyEquals("200", openadjuststockCall.getString("Code"), "验证code");
			
			
			
			ProductDetailsDto productDetailsDto1 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			DetailsCatalogDto catalog = productDetailsDto1.getCatalogList().get(0);
			Logger.verifyEquals(60,catalog.getStock(), "验证总库存");
			Logger.verifyEquals(10,catalog.getRealStock(),"验证真实库存");
			Logger.verifyEquals(50,catalog.getVirtualStock(), "验证虚拟库存");
			
			
//			JsonObject in = Shop2cnStockCallService.GetStockInInventoryList(rootUserId).get(0).getAsJsonObject();
			 JsonObject out = Shop2cnStockCallService.GetStockOutInventoryList(rootUserId).get(0).getAsJsonObject();
//			 Logger.verifyEquals(stockNum, in.get("totalNum").getAsInt(),"验证入库");
			 Logger.verifyEquals(30, out.get("totalNum").getAsInt(),"验证出库");
		} catch (Exception e) {
			Logger.fail(e);
	}
	}
	
	
	
	
}